package com.cn.itcast.gjp.controller;

import com.cn.itcast.gjp.domain.Sort;
import com.cn.itcast.gjp.mapper.SortMapper;
import com.cn.itcast.gjp.view.AbstractOperationSortDialog;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;

public class AddSortController extends AbstractOperationSortDialog {
    public AddSortController(JDialog dialog) {
        super(dialog);
        this.setTitle("添加分类");
        this.titleLabel.setText("添加分类");


    }

    @Override
    public void confirm() {
        //        设置添加数据
        String parent = this.parentBox.getSelectedItem().toString();
        String sname = this.snameTxt.getText();
        String sdesc = this.sdescArea.getText();
        Sort sort = new Sort();
        if (parent.equals("-请选择-") || sname.isEmpty() || sdesc.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请认真填写各项数据！");
            return;
        }

        sort.setSname(sname);
        sort.setSdesc(sdesc);
        sort.setParent(parent);

        //往数据库添加信息
        SqlSession session= MyBatisUtil.getSqlSession();
        SortMapper sortMapper = session.getMapper(SortMapper.class);

        sortMapper.AddSort(sort);
        //提交事务
        session.commit();
        session.close();

        System.out.println(sort);
        //关闭窗口
        this.dispose() ;
        JOptionPane.showMessageDialog(this, "添加成功！");
    }
}
